package com.vito.framework.system.notice.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vito.framework.base.controller.BaseController;
import com.vito.framework.base.exception.CommonException;
import com.vito.framework.rest.bean.AppSession;
import com.vito.framework.system.notice.model.NoticeBean;
import com.vito.framework.system.notice.service.NoticeService;
import com.vito.support.ApiResponse;
import com.vito.support.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***************************************************************
 * Name:        NoticeManageAction.java     
 * Create At:   2010-06-27
 * Module:      系统公告管理
 * Description: [系统管理-系统公告管理功能]
 * Author       liji
 * ------------------------------------------------------------
 * 修改历史
 * 序号  日期  修改人   修改原因
 * 1  2010-06-29 wanght 
 * 2
 ***************************************************************/
@RestController
@RequestMapping("${adminPath}/notice")
public class NoticeController extends BaseController {

	@Autowired
	private NoticeService noticeService;

	/**
	 * Author:liji
	 * Create At:2010-06-27
	 * Description: [系统管理-系统公告管理功能，查询系统公告列表]
	 *
	 * @return String
	 */
	@PostMapping(value = "getList", consumes = "application/json;charset=UTF-8")
	public ApiResponse getList(HttpServletRequest request, @RequestBody JSONObject params) {
		Map<String, Object> paramMap = super.getParamMap(request,params);
		if(null == paramMap) {
			return ApiResponse.of(ResultEnum.PARAMS_INVALID);
		}

		Map<String, Object> resMap = new HashMap<>();
		String param_startDate = params.getString("param_startDate");
		String param_endDate = params.getString("param_endDate");
		if(param_startDate == null) {
			param_startDate = DateUtil.formatDate(DateUtil.offsetDay(new Date(), -7));
			paramMap.put("param_startDate", param_startDate);
		}
		if(param_endDate == null) {
			param_endDate = DateUtil.formatDate(new Date());
			paramMap.put("param_endDate", param_endDate);
		}

		paramMap.put("param_orgId", paramMap.get("cur_orgId"));
		paramMap.put("param_noticeName", params.getString("param_noticeName"));

		int page = params.getIntValue("page");
		int limit = params.getIntValue("limit");
		int pageCount;
		try {
			Page<NoticeBean> pageParam = new Page<>(page, limit);
			return ApiResponse.ofPage(noticeService.getNoticeList(pageParam, paramMap));
		} catch(CommonException e) {
			return ApiResponse.of(e);
		}
	}

	/**
	 * Author:liji
	 * Create At:2010-06-27
	 * Description: [系统管理-系统公告管理功能，批量删除公告]
	 *
	 * @return String
	 */
	@PostMapping(value = "delete", consumes = "application/json;charset=UTF-8")
	public ApiResponse delete(HttpServletRequest request, @RequestBody JSONObject params) {
		Map<String, Object> paramMap = super.getParamMap(request,params);
		if(null == paramMap) {
			return ApiResponse.of(ResultEnum.PARAMS_INVALID);
		}

		List<String> idList = JSONArray.parseArray(params.getJSONArray("ids").toJSONString(), String.class);
		try {
			noticeService.removeByIds(idList);
		} catch(CommonException e) {
			return ApiResponse.of(e);
		}
		return ApiResponse.ok(ResultEnum.OK);
	}

	/**
	 * Author:liji
	 * Create At:2010-06-27
	 * Description: [系统管理-系统公告管理功能，修改系统公告-查询出系统公告的资料]
	 *
	 * @return String
	 */
	@PostMapping(value = "edit", consumes = "application/json;charset=UTF-8")
	public ApiResponse edit(HttpServletRequest request, @RequestBody JSONObject params) {
		Map<String, Object> paramMap = super.getParamMap(request,params);
		if(null == paramMap) {
			return ApiResponse.of(ResultEnum.PARAMS_INVALID);
		}

		Map<String, Object> resMap = new HashMap<>();

		paramMap.put("param_orgId", paramMap.get("cur_orgId"));
		paramMap.put("param_userId", paramMap.get("cur_userId"));

		String noticeId = params.getString("noticeId");
		paramMap.put("noticeId", noticeId);

		String RelstartTime, RelendTime;
		if(!StringUtils.isEmpty(params.getString("param_RelstartTime"))) {
			paramMap.put("param_startDate", params.getString("param_RelstartTime"));
			RelstartTime = (String) paramMap.get("param_RelstartTime");
		} else {
			RelstartTime = (String) paramMap.get("param_startDate");
		} resMap.put("RelstartTime", RelstartTime);

		if(!StringUtils.isEmpty(params.getString("param_RelendTime"))) {
			paramMap.put("param_endDate", params.getString("param_RelendTime"));
			RelendTime = (String) paramMap.get("param_RelendTime");
		} else {
			RelendTime = (String) paramMap.get("param_endDate");
		}
		resMap.put("RelendTime", RelendTime);

		String deleteFlag = params.getString("param_delete");

		try {
			NoticeBean noticeBean = noticeService.getById(noticeId);
			resMap.put("noticeBean", noticeBean);
			noticeService.editNoticeReadFlag(noticeBean);
			resMap.put("method", "edit");
			resMap.put("deleteFlag", deleteFlag);
			return ApiResponse.ok(resMap);
		} catch(CommonException e) {
			return ApiResponse.of(e);
		}
	}

	/**
	 * Author:liji
	 * Create At:2010-06-27
	 * Description: [系统管理-系统公告管理功能，保存修改]
	 *
	 * @return String
	 */
	@PostMapping(value = "editSave", consumes = "application/json;charset=UTF-8")
	public ApiResponse editSave(HttpServletRequest request,@RequestBody JSONObject params, NoticeBean noticeBean) {
		Map<String, Object> paramMap = super.getParamMap(request,params);
		if(null == paramMap) {
			return ApiResponse.of(ResultEnum.PARAMS_INVALID);
		}

		AppSession session = super.getUserSession(request);
		String noticeName = params.getString("noticeName");
		String noticeType = params.getString("noticeType");
		String noticeMsg = params.getString("noticeMsg");
		noticeBean.setNoticeName(noticeName);
		noticeBean.setNoticeType(noticeType);
		noticeBean.setNoticeMsg(noticeMsg);
		noticeBean.setOrgId(session.getMerchantId());
		noticeBean.setOperator(session.getUserName());
		noticeBean.setOperatorId(session.getUserId());
		try {
			noticeService.updateById(noticeBean);
			return ApiResponse.ok(ResultEnum.OK);
		} catch(CommonException e) {
			return ApiResponse.of(e);
		}


	}

	/**
	 * Author:liji
	 * Create At:2010-06-27
	 * Description: [系统管理-系统公告管理功能，保存新添加的系统公告]
	 *
	 * @return String
	 */
	@PostMapping(value = "addSave", consumes = "application/json;charset=UTF-8")
	public ApiResponse addSave(HttpServletRequest request,@RequestBody JSONObject params, NoticeBean noticeBean) {
		Map<String, Object> paramMap = super.getParamMap(request,params);
		if(null == paramMap) {
			return ApiResponse.of(ResultEnum.PARAMS_INVALID);
		}

		String noticeName = params.getString("noticeName");
		String noticeType = params.getString("noticeType");
		String noticeMsg = params.getString("noticeMsg");
		AppSession session = super.getUserSession(request);
		noticeBean.setNoticeName(noticeName);
		noticeBean.setNoticeType(noticeType);
		noticeBean.setNoticeMsg(noticeMsg);
		noticeBean.setOrgId(session.getMerchantId());
		noticeBean.setOperator(session.getUserName());
		noticeBean.setOperatorId(session.getUserId());

		try {
			noticeService.save(noticeBean);
			return ApiResponse.ok(ResultEnum.OK);
		} catch(CommonException e) {
			return ApiResponse.of(e);
		}
	}
}
